//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef __SYSEVENT_H__
#define __SYSEVENT_H__

class CProcess;
class CSystemEvent;

extern CSystemEvent *CreateSystemEvent(CProcess *pOwner, Boolean bManualReset);

extern void DetachSystemEvent(CSystemEvent *pSysEvent);

extern void TriggerSystemEvent(SystemEvent sysEvent);

extern void ClearSystemEvent(SystemEvent sysEvent);

extern void TriggerSystemEvent(CProcess *pOwner, SystemEvent sysEvent);

extern void ClearSystemEvent(CProcess *pOwner, SystemEvent sysEvent);

class CSystemEvent : public DLinkNode, public CEvent
{
public:
    CSystemEvent(CProcess *pOwner, Boolean bManualReset);
    ~CSystemEvent();

    void Interested(UInt32 dwInterestedEvents);
    void Trigger(SystemEvent sysEvent);
    void Clear(SystemEvent sysEvent);

public:
    UInt32       m_dwInterestedEvents;
    CProcess    *m_pOwner;
};

inline CSystemEvent::CSystemEvent(CProcess *pOwner, Boolean bManualReset)
{
    CEvent::Initialize(bManualReset, UNSIGNALED);
    m_pOwner = pOwner;
    m_dwInterestedEvents = 0;
}

inline CSystemEvent::~CSystemEvent()
{
    DetachSystemEvent(this);
}

inline void CSystemEvent::Interested(UInt32 dwInterestedEvents)
{
    m_dwInterestedEvents = dwInterestedEvents;
}

inline void CSystemEvent::Trigger(SystemEvent sysEvent)
{
    if (sysEvent & m_dwInterestedEvents) {
        this->Notify((EventState)sysEvent);
    }
}

inline void CSystemEvent::Clear(SystemEvent sysEvent)
{
    if (sysEvent & m_dwInterestedEvents) {
        this->CEvent::Clear();
    }
}

#endif // __SYSEVENT_H__
